<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogType === 'create' ? '新建任务' : '编辑任务'"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
      label-position="right"
    >
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="formData.taskName" placeholder="请输入任务名称" />
      </el-form-item>

      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="formData.taskType" placeholder="请选择任务类型">
          <el-option label="数据同步" value="SYNC" />
          <el-option label="数据处理" value="PROCESS" />
        </el-select>
      </el-form-item>

      <el-form-item label="任务描述" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="3"
          placeholder="请输入任务描述"
        />
      </el-form-item>

      <el-form-item label="执行参数" prop="params">
        <el-input
          v-model="formData.params"
          type="textarea"
          :rows="3"
          placeholder="请输入JSON格式的执行参数"
        />
      </el-form-item>

      <el-form-item label="定时配置" prop="cronExpression">
        <el-input v-model="formData.cronExpression" placeholder="请输入cron表达式" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { taskApi, type Task } from '@/api/task'

const dialogVisible = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
})
const props = defineProps({
  dialogType: String,
  currentData: Object
})

const emit = defineEmits(['update:dialogVisible', 'refresh'])

const formRef = ref<FormInstance>()
const formData = reactive<Partial<Task>>({
  taskName: '',
  taskType: '',
  description: '',
  params: '',
  cronExpression: ''
})

const rules = reactive<FormRules>({
  taskName: [
    { required: true, message: '请输入任务名称', trigger: 'blur' }
  ],
  taskType: [
    { required: true, message: '请选择任务类型', trigger: 'change' }
  ]
})

// 监听currentData变化
watch(() => props.currentData, (val) => {
  Object.assign(formData, val || {})
}, { immediate: true, deep: true })

// 关闭对话框
const handleClose = () => {
  emit('update:dialogVisible', false)
  formRef.value?.resetFields()
}

// 提交表单
const handleSubmit = async () => {
  try {
    await formRef.value?.validate()
    
    if (props.dialogType === 'create') {
      await taskApi.createTask(formData)
      ElMessage.success('创建成功')
    } else {
      await taskApi.updateTask(formData)
      ElMessage.success('更新成功')
    }
    
    emit('refresh')
    handleClose()
  } catch (error) {
    console.error(error)
  }
}
</script>
